﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.Runtime.Serialization;
using System.Web.Security;
using CLibArtreve.models;
using CLibArtreve.bll;

namespace CLibArtreve.security
{
    [Serializable]
    public class ArtreveIdentity : IIdentity, ISerializable
    {
        //CONNECTIONSTRING
        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        //TICKET
        private FormsAuthenticationTicket _ticket;

        public FormsAuthenticationTicket Ticket
        {
            get { return _ticket; }
            set { _ticket = value; }
        }
        #region IIdentity Members

        public string AuthenticationType
        {
            get { return "Forms"; }
        }

        public bool IsAuthenticated
        {
            get { return true; }
        }

        public string Name
        {
            get
            {
                return Ticket.Name;
            }
        }

        //USER
        private User _user;
        public User User
        {
            get { return _user; }
            set { _user = value; }
        }

        #endregion
        //CONSTRUCTOR
        public ArtreveIdentity(FormsAuthenticationTicket ticket, string connectionString)
        {
            Ticket = ticket;
            //SET CONNECTION STRING
            ConnectionString = connectionString;
            //FILL DATA
            FillData(ticket.Name);
        }
        private void FillData(string username)
        {
            try
            {
                UserBLL bllUsers = new UserBLL(ConnectionString);
                User = bllUsers.GetUserByEmail(username);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        //SERIALIZATION
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //State will be CrossAppDomain for serialization induced by WebDev.WebServer
            if (context.State.Equals(StreamingContextStates.CrossAppDomain))
            {
                var generic = new GenericIdentity(this.Name, this.AuthenticationType);

                info.SetType(generic.GetType());

                var serializableMembers = FormatterServices.GetSerializableMembers(generic.GetType());
                var serializableValues = FormatterServices.GetObjectData(generic, serializableMembers);

                for (int i = 0; i < serializableMembers.Length; i++)
                    info.AddValue(serializableMembers[i].Name, serializableValues[i]);
            }
            else
                throw new InvalidOperationException("Serialization not supported");
        }
    }
}
